package com.supply.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.supply.pojo.Permission;
import com.supply.pojo.ResultBean;
import com.supply.req.PermissionReq;
import com.supply.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;

    @GetMapping("/list")
    public ResultBean list(PermissionReq permissionReq){
//        PageHelper.startPage(permissionReq.getPageNum(),permissionReq.getPageSize());
        List<Permission> permissions = permissionService.queryByCondition(permissionReq);
        return ResultBean.success(permissions);
    }

    @GetMapping("/queryAll")
    public ResultBean queryAll(){
        return ResultBean.success(permissionService.queryAll());
    }


    @GetMapping("/queryRoleAndPermissionByRoleId/{id}")
    public ResultBean queryRoleAndPermissionByRoleId(@PathVariable("id") Long id){
        return ResultBean.success(permissionService.queryRoleAndPermissionByRoleId(id));
    }

    @GetMapping("/queryByRoleId/{id}")
    public ResultBean queryByRoleId(@PathVariable("id") Long id){
        return ResultBean.success(permissionService.queryByRoleId(id));
    }

    @PostMapping("/saveOrUpdate")
    public ResultBean saveOrUpdate(@RequestBody Permission permission){
        System.out.println("新增或修改的数据：" + permission);
        int rs = permissionService.saveOrUpdate(permission);
        return rs >= 1 ? ResultBean.success() : ResultBean.error("服务器异常");
    }

    @DeleteMapping("/delete/{id}")
    public ResultBean delete(@PathVariable("id") Long id){
        System.out.println("要删除的权限id：" + id);
        int rs = permissionService.deleteById(id);
        return rs >= 1 ? ResultBean.success() : ResultBean.error("服务器异常");
    }
}
